package sim;

import java.awt.Container;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DiscreteHistogramm {
	
	Vector<Long> waitingTimes;
	Vector<Long> serviceTimes;
	long waitingTimeMax;
	long waitingTimeMin;
	long serviceTimeMax;
	long serviceTimeMin;
	int binCount;
	long stride;
	int frameLength;
	int frameHeight;
	int xLength;
	int yLength;
	
	long[] countedWaitingCustomers;
	long[] countedServiceCustomers;
	
	public DiscreteHistogramm() {
		waitingTimes = new Vector<Long>(0);
		serviceTimes = new Vector<Long>(0);
		binCount = 10;
		frameLength = 600;
		frameHeight = 300;
		xLength = 290;
		yLength = 290;
	}
	
	public void run(){
		prepareValues();
		
		
		JFrame frame = new JFrame("Discrete Histogramm");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400, 400);
		Container cp = frame.getContentPane();
		JPanel container = new JPanel();
		JPanel waitingCanvas = new DiscreteHistoPanel(countedWaitingCustomers, stride, binCount, xLength, yLength);
		JPanel serviceCanvas = new DiscreteHistoPanel(countedServiceCustomers, stride, binCount, xLength, yLength);
		BoxLayout layout = new BoxLayout(container, BoxLayout.X_AXIS);
		container.setLayout(layout);
		container.add(waitingCanvas);
		container.add(serviceCanvas);
		cp.add(container);
		frame.setVisible(true);
	}
	
	private void prepareValues(){
		int length = waitingTimes.size();
		countedWaitingCustomers = new long[length];
		countedServiceCustomers = new long[length];
		stride = (waitingTimeMax - waitingTimeMin) / binCount;
		for (int i = 0; i < length; i++) {
			countedWaitingCustomers[(int) Math.floor((waitingTimes.get(i)/stride))]++; 
			countedServiceCustomers[(int) Math.floor((serviceTimes.get(i)/stride))]++; 
		}
	}
	
	public void pushValues(Customer c){
		long waitingTime = c.getServiceInitiationTime() - c.getArrivalTime();
		long serviceTime = c.getServiceCompletionTime() - c.getServiceInitiationTime();
		waitingTimeMax = waitingTime > waitingTimeMax ? waitingTime : waitingTimeMax;
		waitingTimeMin = waitingTime < waitingTimeMin ? waitingTime : waitingTimeMin;
		serviceTimeMax = waitingTime > serviceTimeMax ? waitingTime : serviceTimeMax;
		serviceTimeMin = waitingTime < serviceTimeMin ? waitingTime : serviceTimeMin;
		waitingTimes.add(waitingTime);
		serviceTimes.add(serviceTime);
	}
	
	
	public void report(){
		
	}

}
